//
//  ViewController.m
//  Net2
//
//  Created by niit on 16/3/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import <SVProgressHUD.h>
#import <MBProgressHUD.h>
#import <TBXML.h>
#import "NetManager.h"
@interface ViewController ()<NetManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *pswdTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)loginBtnX1Pressed:(id)sender
{
    NetManager *net = [NetManager shareNetManager];
    net.delegate = self;
    [net loginWithUserName:self.userTextField.text andPassword:self.pswdTextField.text];
//    BOOL logined = [NetManager loginWithUserName:self.userTextField.text andPassword:self.pswdTextField.text];
//    
//    if(logined)
//    {
//           
//        [SVProgressHUD showWithStatus:@"登陆成功"];
//               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//           
//            [SVProgressHUD dismiss];
//            
//            [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//        });
//    }
//    else
//    {
//        NSString *message = @"登陆失败";
//       
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [self dismissViewControllerAnimated:YES completion:nil];
//        }];
//        [alert addAction:aciton];
//        [self presentViewController:alert animated:YES completion:nil];
//    }
}

- (void)loginSuccess
{
    [SVProgressHUD showWithStatus:@"登陆成功"];
    // 1s后跳转下以页面
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 隐藏HUD
        [SVProgressHUD dismiss];
        // 下一页
        [self performSegueWithIdentifier:@"gotoMain" sender:nil];
    });
}

- (void)loginFail:(NSError *)error
{
    [SVProgressHUD dismiss];
    NSString *message = [NSString stringWithFormat:@"错误信息:%@",[error localizedDescription]];
    // 登陆失败 显示错误信息
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:aciton];
    [self presentViewController:alert animated:YES completion:nil];
}



//- (IBAction)loginBtnX2Pressed:(id)sender
//{
//    [SVProgressHUD showWithStatus:@"正在登陆"];
//    [NetManager loginWithUserName:self.userTextField.text
//                      andPassword:self.pswdTextField.text
//                     successBlock:^{
//                        
//                         [SVProgressHUD showWithStatus:@"登陆成功"];
//                        
//                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                         
//                             [SVProgressHUD dismiss];
//                        
//                             [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//                         });
//                     } failedBlock:^(NSError *error){
//                         [SVProgressHUD dismiss];
//                         NSString *message = [NSString stringWithFormat:@"错误信息:%@",[error localizedDescription]];
//                       
//                         UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//                         UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                             [self dismissViewControllerAnimated:YES completion:nil];
//                         }];
//                         [alert addAction:aciton];
//                         [self presentViewController:alert animated:YES completion:nil];
//                     }];
//    
//    
//}


- (IBAction)loginBtn:(id)sender
{
//    //http://localhost:8080/MJServer/
//    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/username=%@&pwd=%@&method=get&type=JSON",self.userTextField.text,self.pswdTextField.text];
//    NSURL *url = [NSURL URLWithString:str];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    
//    [SVProgressHUD showWithStatus:@"正在登陆"];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        if(connectionError == nil)
//        {
//            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@",result);
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//            
//            if (dict[@"error"]!=nil)
//            {
//                [SVProgressHUD dismiss];
//                NSString *message = [NSString stringWithFormat:@"错误信息%@",dict[@"error"]];
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    [self dismissViewControllerAnimated:YES completion:nil];
//                }];
//                [alert addAction:action];
//                [self presentViewController:alert animated:YES completion:nil];
//            }
//            else
//            {
//                [SVProgressHUD showWithStatus:@"登陆成功"];
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [SVProgressHUD dismiss];
//                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//                });
//            }
//        }
//        
//        else
//        {
//            NSLog(@"%@",connectionError,[connectionError localizedDescription]);
//            NSString *message = [NSString stringWithFormat:@"连接错误:%@",[connectionError localizedDescription]];
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                [self dismissViewControllerAnimated:YES completion:nil];
//                
//            }];
//            [alert addAction:action];
//            [self presentViewController:alert animated:YES completion:nil];
//        }
//    }];
//    
//}
//
//    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",self.userTextField.text,self.pswdTextField.text];
//    // 请求网址
//    NSURL *url = [NSURL URLWithString:str];
//    // 请求
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    
//    [SVProgressHUD showWithStatus:@"正在登陆"];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//    
//    // 发送异步请求
//    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        
//        if(connectionError == nil)
//        {
//            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@",result);
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//            
//            //{"success":"登录成功"}
//            //{"error":"密码不正确"}
//            if(dict[@"error"] != nil)
//            {
//                // 隐藏HUD
//                [SVProgressHUD dismiss];
//                
//                NSString *message = [NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
//                // 登陆失败 显示错误信息
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//                UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    [self dismissViewControllerAnimated:YES completion:nil];
//                }];
//                [alert addAction:aciton];
//                [self presentViewController:alert animated:YES completion:nil];
//                
//                
//            }
//            else
//            {
//                // 提示登陆成功
//                [SVProgressHUD showWithStatus:@"登陆成功"];
//                // 1s后跳转下以页面
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    // 隐藏HUD
//                    [SVProgressHUD dismiss];
//                    // 条状下一页
//                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//                });
//            }
//            
//        }
//        else
//        {
//            NSLog(@"%@",connectionError,[connectionError localizedDescription]);
//            NSString *message = [NSString stringWithFormat:@"连接错误:%@",[connectionError localizedDescription]];
//            
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                [self dismissViewControllerAnimated:YES completion:nil];
//            }];
//            [alert addAction:aciton];
//            [self presentViewController:alert animated:YES completion:nil];
//        }
//        
//    }];
//    
}


- (IBAction)loginBtn2Pressed:(id)sender
{

    
//    NSString *str = @"http://localhost:8080/MJServer/login";
//    NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@&type=JSON",self.userTextField.text,self.pswdTextField.text];
//    NSURL *url = [NSURL URLWithString:str];
//    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//    urlRequest.HTTPMethod = @"POST";
//    urlRequest.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];//NSString -> NSData
//    
//    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        
//        if(connectionError == nil)
//        {
//            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@",result);
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//            
//            //            {"success":"登录成功"}
//            
//            //{"error":"密码不正确"}
//            if(dict[@"error"] != nil)
//            {
//                // 登陆失败 显示错误信息
//                NSString *message = [NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//                UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    [self dismissViewControllerAnimated:YES completion:nil];
//                }];
//                [alert addAction:aciton];
//                [self presentViewController:alert animated:YES completion:nil];
//            }
//            else
//            {
//                // 添加加ProgressHUD
//                [SVProgressHUD showWithStatus:@"登陆成功"];
//                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//                dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//                dispatch_async(queue, ^{
//                    sleep(1);
//                });
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [SVProgressHUD dismiss];
//                });
//
//                // 提示登陆成功
//                // 1s
//                
//                // 登陆成功 跳转到下一页
//                [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//                
//            }
//            
//        }
//                
//    }];
    
}

// POST XML
- (IBAction)loginBtn3Pressed:(id)sender
{
    
//    NSString *str = @"http://localhost:8080/MJServer/login";
//    NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@&type=XML",self.userTextField.text,self.pswdTextField.text];
//    NSURL *url = [NSURL URLWithString:str];
//    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//    urlRequest.HTTPMethod = @"POST";
//    urlRequest.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];//NSString -> NSData
//    
//    // 显示
//    [SVProgressHUD showWithStatus:@"正在登陆"];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        
//        if(connectionError == nil)
//        {
//            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@",result);
//            //<error>密码不正确</error> // 当前就一个节点
//            //<success>登录成功</success>
//            
//            TBXML *xml = [[TBXML alloc] initWithXMLData:data error:nil];
//            
//            TBXMLElement *root = xml.rootXMLElement;// 得到根节点
//            NSString *elementName = [TBXML elementName:root];// 节点名字
//            NSString *info = [TBXML textForElement:root];// 节点里的内容
//            NSLog(@"%@",info);
//            
//            if([elementName isEqualToString:@"error"])// 判断节点名字是success还是error
//            {
//                // 隐藏
//                [SVProgressHUD dismiss];
//                
//                // 登陆失败 显示错误信息
//                NSString *message = [NSString stringWithFormat:@"错误信息:%@",info];
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
//                UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    [self dismissViewControllerAnimated:YES completion:nil];
//                }];
//                [alert addAction:aciton];
//                [self presentViewController:alert animated:YES completion:nil];
//            }
//            else
//            {
//                [SVProgressHUD showInfoWithStatus:@"登陆成功!"];
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [SVProgressHUD dismiss];
//                    // 登陆成功
//                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
//                });
//                
//                
//            }
//            
//        }
//        else
//        {
//            // 连接错误
//            NSLog(@"%@",connectionError,[connectionError localizedDescription]);
//        }
//        
//    }];
//    
}

@end
